use honeybee::prelude::*;
use honeybee::Result;

use crate::app_node_manager::{Manager, Register, Unregister};

pub struct AppListenDelegate;
#[async_trait::async_trait]
impl ListenDelegate for AppListenDelegate {
    async fn on_started(&self, stream: &mut TcpStreamActor) -> Result<()> {
        let app_node_manager = Manager::from_registry().await?;
        app_node_manager.send(Register(
            stream.remote_node_name().into(),
            stream.self_addr(),
        ))
    }

    async fn on_stoped(&self, stream: &mut TcpStreamActor) -> Result<()> {
        let app_node_manager = Manager::from_registry().await?;
        app_node_manager.send(Unregister(stream.remote_node_name().into()))
    }
}
